﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FluentValidation;
using FluentValidation.Results;
using Microhard.Plat.CRM.Customers.Models;
using Microhard.Plat.CRM.Customers.Msg;
using Microhard.Plat.CRM.Customers.Sys;


namespace Microhard.Plat.CRM.Customers.Validator
{
    public class CustomerOrderModelValidator : AbstractValidator<OrderModel>
    {
        public CustomerOrderModelValidator()
        {
            RuleSet("Insert", () =>
            {
                RuleFor(u => u.DeliveryAddress).NotEmpty().WithMessage(OrderMsg.NeedDeliveryAddress).When(r => r.OrderID == Guid.Empty);
                Custom(u =>
                {
                    if (u.OrderTypeSelect == OrderType.CustomerOrder && u.CustomerID == Guid.Empty)
                    {
                        return new ValidationFailure("CustomerID", OrderMsg.NeedCustomer);
                    }
                    if (u.OrderTypeSelect == OrderType.KAOrder && u.FromKAID == Guid.Empty)
                    {
                        return new ValidationFailure("FromKAID", OrderMsg.NeedCustomer);
                    }
                    return null;
                });
            });

            RuleSet("Update", () =>
            {
                RuleFor(u => u.DeliveryAddress).NotEmpty().WithMessage(OrderMsg.NeedDeliveryAddress).When(r => r.OrderID == Guid.Empty);
                Custom(u =>
                {
                    if (u.OrderTypeSelect == OrderType.CustomerOrder && u.CustomerID == Guid.Empty)
                    {
                        return new ValidationFailure("CustomerID", OrderMsg.NeedCustomer);
                    }
                    if (u.OrderTypeSelect == OrderType.KAOrder && u.FromKAID == Guid.Empty)
                    {
                        return new ValidationFailure("FromKAID", OrderMsg.NeedCustomer);
                    }
                    return null;
                });
            });
        }
    }
}
